package br.com.jsfprimefaces.util;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.com.jsfprimefaces.dao.FilmeDAO;
import br.com.jsfprimefaces.model.Filme;

@FacesConverter(value="converterFilme", forClass= Filme.class) 
public class FilmeConverter implements javax.faces.convert.Converter{
	
	/*@Override
	public Object getAsObject (FacesContext facesContext, UIComponent component, String submittedValue) {  
        if (submittedValue.trim().equals("")) {  
            return null;  
        } else {  
            try {  
                FilmeDAO filmeDAO = new FilmeDAO();
                
            	
                for (Filme f : filmeDAO.searchByName(submittedValue)) {  
                    if (f.getNome() == submittedValue) {  
                        return f;  
                    }  
                }  
                  
            } catch(NumberFormatException exception) {  
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));  
            }  
        }  
  
        return null;  
    }*/
	
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String key) {
			
		FilmeDAO filmeDAO = new FilmeDAO();
			
		return filmeDAO.searchByName(key);
	}
	
	@Override
	  public String getAsString(FacesContext facesContext, UIComponent component, Object value) {  
	        if (value == null || value.equals("")) {  
	            return "";  
	        } else {  
	            /*return String.valueOf(((Filme) value).getNome());*/
	        	Filme filme = (Filme) value;
	        	System.out.println(String.valueOf(filme.getNome()));
	        	return String.valueOf(filme.getNome());
	        	/*
	        	return value.toString();*/
	        }  
	    }

    
}
